﻿#include "msgserver.h"
#include <QDataStream>

MsgServer::MsgServer(QObject *parent) : QObject(parent)
{
    m_tcpServer = new QTcpServer(this);
    connect(m_tcpServer, SIGNAL(newConnection()),
            this, SLOT(slotNewConnection()));

    m_tcpServer->listen(QHostAddress::Any, GlobalVars::g_localConfig.readPort().toUShort());
    m_msgProcess = new MsgProcess();
    connect(m_msgProcess, SIGNAL(signalSendMsgToClient(QString,QString)),
            this, SLOT(slotSendMsgToClient(QString,QString)));
    connect(m_msgProcess, SIGNAL(signalSendImageToClient(QString,QString,QString)),
            this, SLOT(slotSendImageToClient(QString,QString,QString)));
    connect(m_msgProcess, SIGNAL(signalShowImage(QImage)),
            this, SIGNAL(signalShowImageServer(QImage)));
    connect(m_msgProcess, SIGNAL(signalSaveCommentImage(const QString&,const QImage&)),
            this, SIGNAL(signalSaveCommentImage(const QString&,const QImage&)));
    connect(m_msgProcess, SIGNAL(signalSaveHouseImage(const QString&,const QImage&)),
            this, SIGNAL(signalSaveHouseImage(const QString&,const QImage&)));
    connect(m_msgProcess, SIGNAL(signalSaveHotelImage(const QString&,const QImage&)),
            this, SIGNAL(signalSaveHotelImage(const QString&,const QImage&)));
    connect(m_msgProcess, SIGNAL(signalSaveUserHeadImage(const QString&,const QImage&)),
            this, SIGNAL(signalSaveUserHeadImage(const QString&,const QImage&)));
    connect(m_msgProcess, SIGNAL(signalSaveUserCertImage(const QString&,const QImage&)),
            this, SIGNAL(signalSaveUserCertImage(const QString&,const QImage&)));
    connect(m_msgProcess, SIGNAL(signalUploadPicSuccessed()),
            this, SIGNAL(signalUploadPicSuccessed()));

    m_msgProcess->start();
}

void MsgServer::slotNewConnection()
{
    qDebug() << "MsgServer::slotNewConnection()";
    QTcpSocket *socket = m_tcpServer->nextPendingConnection();
    m_msgSocket = new MsgSocket(socket);
    connect(m_msgSocket, SIGNAL(signalRegisterSocket(QString,MsgSocket*)),
            this, SLOT(slotRegisterSocket(QString,MsgSocket*)));
    connect(m_msgSocket, SIGNAL(signalLogoutSocket(QString,MsgSocket*)),
            this, SLOT(slotLogoutSocket(QString,MsgSocket*)));
}

void MsgServer::slotRegisterSocket(QString id, MsgSocket *socket)
{
    m_socketMap.insert(id, socket); //登录成功，添加Socket到g_socketMap
}

void MsgServer::slotLogoutSocket(QString id, MsgSocket *socket)
{
    qDebug() << "exit" << id;
    if(m_socketMap.contains(id))
    {
        m_socketMap.remove(id);
    }
}
void MsgServer::slotSendMsgToClient(QString id,QString msg)
{
    if(m_socketMap.contains(id))            ///找到对应id 的ip 后发送msg
    {
        m_socketMap[id]->slotSendMsg(msg);
    }
}

void MsgServer::slotSendImageToClient(QString id, QString msg,QString path)
{
    qDebug() << "slotSendImageToClient" << path << id;
    if(m_socketMap.contains(id))            ///找到对应id 的ip 后发送image
    {
        QImage image(path);
        qDebug() << "image.size() " << image.size();
        signalShowImageServer(image);
        m_socketMap[id]->slotSendImage(msg,path);
    }
}



